package Command::Basic;

use Moose;
use Data::Dumper;
use Web::Scraper;
use LWP::Useragent;
use Net::Twitter;
use Me2day;
use Encode qw/encode decode/;

with 'Command';

has 'func_public_cmd' => (
	is		=> 'ro',
	default	=> sub {
		{
			kick	=> \&cmd_public_kick,
			j2k		=> \&cmd_public_j2k,
			help	=> \&cmd_public_help,
			dic		=> \&cmd_public_dic,
			map		=> \&cmd_public_map,
			search	=> \&cmd_public_search,
			lang	=> \&cmd_public_lang,
			twitter => \&cmd_public_twitter,
			me2day  => \&cmd_public_me2day,
		}
	},
);

has 'twitter' => (
	is		=> 'ro',
	lazy	=> 1,
	default => sub {
		my $self = shift;
		if(exists($self->config->{twitter})) {
			return Net::Twitter->new( 
		        username => $self->config->{twitter}->{username}, 
		        password => $self->config->{twitter}->{password},
		    );
		}
	},
);

# $irc, $nic, $channel, $cmd, $desc
sub do_command {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	
	warn "do_command : cmd[$cmd], desc[$desc], channel[$channel], nick[$nick]\n";
	
	return $irc->yield( privmsg => "#".$channel => $nick." : ".$self->config->{command}->{$cmd} )
		if !$desc and !($cmd =~ /(?:help|nolog)/);

	return $self->func_public_cmd->{$cmd}->(@_)
		if exists($self->func_public_cmd->{$cmd});

	return 0;
};

# $irc, $nick, $channel, $cmd, $desc
sub cmd_public_help {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;

	foreach my $comment (@{ $self->config->{command}->{help} }) {
		$irc->yield( privmsg => "#".$channel => $comment );
	}
}

sub cmd_public_kick {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	if($desc) {
		$irc->yield( kick => "#".$channel => $desc );
	}
}

sub cmd_public_j2k {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;

	return
		unless exists($self->config->{translator})
	       and exists($self->config->{translator}->{url});

	my $address = $self->config->{translator}->{url};

	if ($desc) { 
		$address .= $desc;
	}
	# 번역할 url이 주어지지 않으면 가장 최근에 인식한 url을 자동으로 사용한다.
#	elsif ($base_url) {
#		$address .= $base_url;
#	}
	else {
		return;
	}
	$irc->yield( privmsg => "#".$channel => $address );
}


sub cmd_public_dic {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	my $html = scraper {
		process 'table>tr>td[class="p3"]', text => 'TEXT';
	};
	
	my $arg = encode("euc-kr", decode("utf-8", $desc));
	my $data = $html->scrape(URI->new($self->config->{dictionary}->{url} . URI::Escape::uri_escape($arg)));
	
	my @voca = split /\d/, $data->{text};
	
	for my $dic (@voca) {
		$irc->yield( privmsg => "#".$channel => $dic );
	}
}

sub cmd_public_map {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	my $arg = encode("euc-kr", decode("utf-8", $desc));
	my $address = $self->config->{navermap}->{url} . URI::Escape::uri_escape($arg);
	$irc->yield( privmsg =>"#".$channel=>$address);
}

sub cmd_public_search {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	my $html = scraper {
		process 'div[class="g"]', text => 'TEXT';
	};
	
	my $data = $html->scrape(URI->new($self->config->{search_google}->{url}.URI::Escape::uri_escape($desc)));
	
	my @entry = split /\d/, $data->{text};
		
	for my $content (@entry) {
		$irc->yield( notic => "#".$channel => $content );
	}
}

sub cmd_public_lang {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	if (!$desc || $desc !~ /^(?:en|ko|ja)$/) {
		$irc->yield( privmsg => "#".$channel => $nick." : ".$self->config->{command}->{lang} );
	}
	else {
		$irc->yield( privmsg => "#".$channel => $nick." : ".$self->config->{log}->{lang}. ' => ' . $desc);
	}
}

sub cmd_public_twitter {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	if($self->twitter) {
		$self->twitter->update($nick. ": ".$desc);
		$irc->yield( privmsg => "#".$channel => $nick ." : ". $self->config->{log}->{twitter} );
	}
}

sub cmd_public_me2day {
	my ($self, $irc, $nick, $channel, $cmd, $desc) = @_;
	my $res;
	if($self->me2day) {
		if ($desc =~ /@\:/) {
			my ($post, $tag) = split /@\:/, $desc; #/
			$tag .= " me2irc";
			$res = $self->me2day->create_post( body => $nick." : ".$post, tags => $tag );
		}
		else {
			$res = $self->me2day->create_post( body => $nick." : ".$desc, tags => "me2irc" );
		}
		if ($res->status_line =~ /200/) {
			$res->content =~ /<permalink>(.*?)<\/permalink>/;
			my $permalink = $1;
			$irc->yield( privmsg => "#".$channel => $nick ." : ".$self->config->{log}->{me2day}. " ".$permalink );
		}
	}
}

1;

